﻿Imports System.Configuration

Public Class frmArtist
    Dim source As DataRowView

    Public Const ARTISTIMAGE_URL As String = "http://musicimage.xboxlive.com/catalog/music.{0}/image?locale=en-US&w=100&h=100&target=circle"

    Public Sub New(dr As DataRowView)
        InitializeComponent()

        source = dr

    End Sub

    Private Sub frmPerson_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        FillForm()

    End Sub

    Private Sub imgServiceMediaID_Click(sender As Object, e As EventArgs) Handles imgServiceMediaID.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Application.DoEvents()

            OpenSearch()

        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub

    Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            Application.DoEvents()

            SaveForm()

            Me.Close()

        Catch ex As Exception
            MsgBox("Failed to save Artist data: " & ex.Message)

        Finally
            Me.Cursor = Cursors.Default

        End Try

    End Sub

    Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
        Me.Close()

    End Sub

    Private Sub FillForm()
        txtName.Text = CStr(source("Name"))
        txtSortName.Text = CStr(source("SortName"))

        If Not IsDBNull(source("ServiceMediaID")) Then
            GetImage(DirectCast(source("ServiceMediaID"), Guid).ToString)
        End If

    End Sub

    Private Sub OpenSearch()
        Dim f As frmArtistSearch

        f = New frmArtistSearch
        f.txtSearchText.Text = txtName.Text

        If f.ShowDialog(Me) = DialogResult.OK AndAlso f.lvResults.SelectedItems.Count > 0 Then
            imgServiceMediaID.Image = f.lvResults.SelectedItems(0).ImageList.Images.Item(f.lvResults.SelectedItems(0).ImageKey)
            source("ServiceMediaID") = f.lvResults.SelectedItems(0).SubItems("ZuneId").Text
        End If

        f.Dispose()

    End Sub

    Private Sub SaveForm()
        Dim sortChar As Char
        Dim sortGroupIndex As Integer

        If Not String.IsNullOrWhiteSpace(txtName.Text) Then sortChar = txtName.Text.TrimStart.Chars(0)
        If Not String.IsNullOrWhiteSpace(txtSortName.Text) AndAlso Not txtSortName.Text.Equals(txtName.Text) Then sortChar = txtSortName.Text.TrimStart.Chars(0)

        If Char.IsLetter(sortChar) Then
            sortGroupIndex = 2 + Asc(Char.ToUpper(sortChar)) - Asc("A"c)

        ElseIf Char.IsDigit(sortChar) Then
            sortGroupIndex = 1

        ElseIf Char.IsSurrogate(sortChar) Then
            sortGroupIndex = 28

        Else
            sortGroupIndex = 0

        End If

        source("Name") = txtName.Text
        source("SortName") = txtSortName.Text
        source("SortNameGroupIndex") = sortGroupIndex

        ESE.UpdateRow(source.Row)

    End Sub

    Private Sub GetImage(id As String)
        Dim wc As Net.WebClient = Nothing

        wc = New Net.WebClient

        AddHandler wc.DownloadDataCompleted, Sub(s As Object, e As Net.DownloadDataCompletedEventArgs)
                                                 Dim img As Bitmap
                                                 If e.Error Is Nothing Then
                                                     img = DirectCast(Bitmap.FromStream(New IO.MemoryStream(e.Result)), Bitmap)
                                                     imgServiceMediaID.Image = img
                                                 End If

                                             End Sub

        wc.DownloadDataAsync(New Uri(String.Format(ARTISTIMAGE_URL, id)))

    End Sub

End Class

